package com.example.demo.repository;

import com.example.demo.entity.SeataStorage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zxk
 */
public interface StorageRepository extends JpaRepository<SeataStorage, Integer> {

    /**
     * 减库存
     * @param commodityCode
     * @param count
     * @return 减库存结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("update SeataStorage set storageCount=storageCount-:count where commodityCode=:commodityCode and storageCount-:count>=0")
    int deduct(@Param("commodityCode") String commodityCode,@Param("count") int count);

    /**
     * 获取库存
     * @param commodityCode
     * @return
     */
    int countByCommodityCode(String commodityCode);
}
